package com.xiaoyi.intentsdklibrary.Intent;

import android.content.Context;
import com.xiaoyi.intentsdklibrary.Bean.AppBeanSqlUtil;
import com.xiaoyi.intentsdklibrary.Bean.IntentBean;
import com.xiaoyi.intentsdklibrary.Bean.IntentBeanSqlUtil;
import com.xiaoyi.intentsdklibrary.Bean.PhoneBeanSqlUtil;
import com.xiaoyi.intentsdklibrary.Utils.ApplicationInfoUtil;
import com.xiaoyi.intentsdklibrary.Utils.ArrayGson;
import com.xiaoyi.intentsdklibrary.Utils.MathUtils;
import com.xiaoyi.intentsdklibrary.Utils.PhoneNumUtil;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.regex.Pattern;

public class IntentSDK {
    private static final int CPU_COUNT;
    private static final int corePoolSize;
    private static final int maximumPoolSize;
    private static final IntentSDK ourInstance = new IntentSDK();
    private ExecutorService fixedThreadPool = Executors.newFixedThreadPool(CPU_COUNT + 1);
    private List<IntentBean> mIntentList;

    static {
        int availableProcessors = Runtime.getRuntime().availableProcessors();
        CPU_COUNT = availableProcessors;
        corePoolSize = Math.max(2, Math.min(availableProcessors - 1, 4));
        maximumPoolSize = (availableProcessors * 2) + 1;
    }

    public static IntentSDK getInstance() {
        return ourInstance;
    }

    private IntentSDK() {
    }

    public void initData(Context context) {
        initSystemControl(context);
    }

    private void initSystemControl(final Context context) {
        this.fixedThreadPool.execute(new Runnable() {
            /* class com.xiaoyi.intentsdklibrary.Intent.IntentSDK.AnonymousClass1 */

            public void run() {
                AppBeanSqlUtil.getInstance().addList(ApplicationInfoUtil.getAllApp(context));
                PhoneBeanSqlUtil.getInstance().addList(PhoneNumUtil.getPhoneData(context));
                IntentBeanSqlUtil.getInstance().addList(new ArrayGson().fromJsonList(RegexData.systenControl, IntentBean.class));
                IntentBeanSqlUtil.getInstance().addList(new ArrayGson().fromJsonList(RegexData.voiceControl, IntentBean.class));
            }
        });
    }

    public List<IntentBean> judge(String str) {
        if (this.mIntentList == null) {
            this.mIntentList = IntentBeanSqlUtil.getInstance().searchAll();
        }
        ArrayList arrayList = new ArrayList();
        for (IntentBean intentBean : this.mIntentList) {
            IntentBean match = match(str, intentBean);
            if (match != null) {
                arrayList.add(match);
            }
        }
        return arrayList;
    }

    public static IntentBean match(String str, IntentBean intentBean) {
        if (Pattern.compile(intentBean.getRegex()).matcher(str).find()) {
            return intentBean;
        }
        return null;
    }

    private static IntentBean findLeverTop(List<IntentBean> list) {
        Collections.sort(list, new Comparator<IntentBean>() {
            /* class com.xiaoyi.intentsdklibrary.Intent.IntentSDK.AnonymousClass2 */

            public int compare(IntentBean intentBean, IntentBean intentBean2) {
                int parseInt = MathUtils.parseInt(intentBean.getLevel());
                int parseInt2 = MathUtils.parseInt(intentBean2.getLevel());
                if (parseInt > parseInt2) {
                    return 1;
                }
                return parseInt == parseInt2 ? 0 : -1;
            }
        });
        return list.get(0);
    }
}
